import type { Module } from 'vuex'
import type { keepAlivePageType } from "../types";

const keepAliveModule: Module<keepAlivePageType,any> = {
  namespaced: true,
  state: {
    // 缓存页面keepAlive
    keepAliveNames: [],
  },
  mutations:{},
  actions: {
    // 添加或删除某个缓存页面
    keepAliveOperate({state},{ mode, name }) {
      switch (mode) {
        case "add":
          state.keepAliveNames.push(name);
          state.keepAliveNames = [...new Set(state.keepAliveNames)];
          break;
        case "delete":
          // eslint-disable-next-line no-case-declarations
          const delIndex = state.keepAliveNames.findIndex(v => v === name);
          delIndex !== -1 && state.keepAliveNames.splice(delIndex, 1);
          break;
      }
    },
    // 清空缓存页面
    clearAllKeepAlivePage({state}) {
      state.keepAliveNames = [];
    }
  }
};
export default keepAliveModule